<html>
<head><meta charset="utf-8"><title>get ast from parser · t-compiler/help · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/index.html">t-compiler/help</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/get.20ast.20from.20parser.html">get ast from parser</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="225811897"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/get%20ast%20from%20parser/near/225811897" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aman Arora <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/get.20ast.20from.20parser.html#225811897">(Feb 10 2021 at 09:31)</a>:</h4>
<p>Is there a way to get the AST from Parser created from <a href="https://doc.rust-lang.org/nightly/nightly-rustc/rustc_parse/fn.new_parser_from_file.html">https://doc.rust-lang.org/nightly/nightly-rustc/rustc_parse/fn.new_parser_from_file.html</a>?</p>



<a name="225854502"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/get%20ast%20from%20parser/near/225854502" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Eric Huss <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/get.20ast.20from.20parser.html#225854502">(Feb 10 2021 at 15:41)</a>:</h4>
<p><code>parser.parse_crate_mod()</code> is the normal entry point.</p>



<a name="225901704"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/get%20ast%20from%20parser/near/225901704" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aman Arora <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/get.20ast.20from.20parser.html#225901704">(Feb 10 2021 at 20:24)</a>:</h4>
<p>thanks, if I wanted to modify the AST externally how do I go about it? Eg: If I want to introduce a print statement at some point in  a given source file by modifying the AST?</p>



<a name="225904376"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/get%20ast%20from%20parser/near/225904376" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aman Arora <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/get.20ast.20from.20parser.html#225904376">(Feb 10 2021 at 20:42)</a>:</h4>
<p>or is there is a way of getting the line number from span? Because then I can edit then use a script to add print on line certain numbers. This might have edge cases around if statements <span aria-label="thinking" class="emoji emoji-1f914" role="img" title="thinking">:thinking:</span></p>



<a name="225905546"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/get%20ast%20from%20parser/near/225905546" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aman Arora <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/get.20ast.20from.20parser.html#225905546">(Feb 10 2021 at 20:50)</a>:</h4>
<p>Actually I think I got the second one,<br>
Leaving them here in case anyone else stumbles on theses</p>
<ul>
<li>
<p><a href="https://doc.rust-lang.org/nightly/nightly-rustc/rustc_session/parse/struct.ParseSess.html#method.source_map">ParseSess::source_map()</a> -&gt; SourceMap</p>
</li>
<li>
<p><a href="https://doc.rust-lang.org/nightly/nightly-rustc/rustc_span/source_map/struct.SourceMap.html#method.span_to_lines">SourceMap::span_to_lines</a> -&gt; Vec&lt;LineInfo&gt;</p>
</li>
</ul>
<p>If someone knows of a better way of adding changes to a source file please let me know <span aria-label="smile" class="emoji emoji-1f642" role="img" title="smile">:smile:</span></p>



<a name="225906734"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/get%20ast%20from%20parser/near/225906734" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Tyson Nottingham <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/get.20ast.20from.20parser.html#225906734">(Feb 10 2021 at 20:58)</a>:</h4>
<p>For the span to lines lookup, you might also look at <code>CachingSourceMapView</code></p>



<a name="225926804"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/get%20ast%20from%20parser/near/225926804" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Eric Huss <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/get.20ast.20from.20parser.html#225926804">(Feb 10 2021 at 23:15)</a>:</h4>
<p>I don't know what you are trying to accomplish, but if it is just modifying source files, you may want to look at something like <a href="https://github.com/google/rerast">rerast</a>.  If you really want to parse it yourself, I would use <code>syn</code> instead of <code>rustc_ast</code>.  Using rustc crates can be a pain, but <code>syn</code> is extremely stable and easy to use.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>